layui.extend({
    admin: '{/}../../static/js/admin'
});
var tabledata =  [{
    "apply_id":11101,
    "apply_classroom_id": "6-2-108",
    "apply_date": "2018-12-03 10:10:11",
    "user_date": "2018-12-29",
    "purpose":"模拟考试",
    "pass_time":"2018-12-05 10:10:11"
}, {
    "apply_id":11102,
    "apply_classroom_id": "11-1-306",
    "apply_date": "2018-12-5 13:56:11",
    "user_date": "2018-12-15",
    "purpose":"组织自习",
    "pass_time":"2018-12-08 10:10:11"
}];

drawtable(tabledata);
function drawtable(tabledata) {
    layui.use(['table', 'jquery', 'form', 'admin'], function () {
        var table = layui.table,
            $ = layui.jquery,
            form = layui.form,
            admin = layui.admin;

        table.render({
            elem: '#articleList',
            cellMinWidth: 80,
            cols: [
                [ {
                    field: 'apply_id', title: '申请编号'
                },  {
                    field: 'apply_classroom_id', title: '教室ID', templet: '#usernameTpl'
                }, {
                    field: 'apply_date', title: '申请时间', sort: true
                }, {
                    field: 'user_date', title: '使用时间', sort: true
                }, {
                    field: 'purpose', title: '教室用途', sort: true
                }, {
                    field: 'pass_time', title: '通过时间'
                }]
            ],
            data: tabledata,
            event: true,
            page: true
        });
        /*
         *数据表格中form表单元素是动态插入,所以需要更新渲染下
         * http://www.layui.com/doc/modules/form.html#render
         * */
        $(function () {
            form.render();
        });

        var active = {

            getCheckData: function () { //获取选中数据
                var apply_id_Array= {};
                var checkStatus = table.checkStatus('articleList'),
                    data = checkStatus.data;
                console.log(data);
                //layer.alert(JSON.stringify(data));
                if (data.length > 0) {
                    layer.confirm('确认要删除这些申请吗？', function (index) {
                        layer.msg('删除成功', {
                            icon: 1
                        });

                        //找到所有被选中的，发异步进行删除
                        //$(".layui-table-body .layui-form-checked").apply_status="已拒绝";
                        for (var i = 0; i < data.length; i++) {
                            apply_id_Array[i]=data[i].apply_id;
                            //console.log("a:" + data[i].recommend);
                            console.log(data[i].apply_status);
                            data[i].apply_status = "未通过";
                            console.log(data[i].apply_status);
                            form.render();
                        }
                        console.log(JSON.stringify(apply_id_Array).toString());
                        dispass(JSON.stringify(apply_id_Array).toString());
                    });
                } else {
                    layer.msg("请先选择处理的申请！");
                }
            },

            getPossData: function () { //获取选中数据
                var apply_id_Array = {};
                var checkStatus = table.checkStatus('articleList'),
                    data = checkStatus.data;
                console.log(data);
                //layer.alert(JSON.stringify(data));
                if (data.length > 0) {
                    layer.confirm('确认要通过这些申请吗？', function (index) {
                        layer.msg('处理完成', {
                            icon: 1
                        });

                        //找到所有被选中的，发异步进行删除
                        //$(".layui-table-body .layui-form-checked").apply_status="已拒绝";
                        for (var i = 0; i < data.length; i++) {
                            //console.log("a:" + data[i].recommend);
                            apply_id_Array[i]=data[i].apply_id;
                            console.log(data[i].apply_status);
                            data[i].apply_status = "已通过";
                            console.log(data[i].apply_status);
                            form.render();
                        }
                        pass(JSON.stringify(apply_id_Array).toString());
                    });
                } else {
                    layer.msg("请先选择处理的申请！");
                }

            },



            Recommend: function () {
                var checkStatus = table.checkStatus('articleList'),
                    data = checkStatus.data;
                if (data.length > 0) {
                    layer.msg("您点击了推荐操作");
                    for (var i = 0; i < data.length; i++) {
                        console.log("a:" + data[i].recommend);
                        data[i].recommend = "checked";
                        console.log("aa:" + data[i].recommend);
                        form.render();
                    }

                } else {
                    console.log("b");
                    layer.msg("请先选择");
                }

                //$(".layui-table-body .layui-form-checked").parents('tr').children().children('input[name="zzz"]').attr("checked","checked");
            },
            Top: function () {
                layer.msg("您点击了置顶操作");
            },
            Review: function () {
                layer.msg("您点击了审核操作");
            }

        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /*用户-删除*/
        window.member_del = function (obj, id) {
            layer.confirm('确认要删除驳回这些申请吗？', function (index) {
                //发异步删除数据
                //$(obj).parents("tr").remove();
                layer.msg('已删除!', {
                    icon: 1,
                    time: 1000
                });
            });
        }

    });
}
function delAll(argument) {
    var data = tableCheck.getData();
    layer.confirm('确认要拒绝这些申请吗？' , function(index) {
        //捉到所有被选中的，发异步进行删除
        layer.msg('处理成功', {
            icon: 1
        });
        //$(".layui-form-checked").not('.header').parents('tr').remove();
    });
}

function search(user_Id){
    $.ajax({
        dataType:"json",
        type:"post",
        url:"/history_my_waiting.action",//修改申请编号为data的一系列申请信息 管理员处理状态改为 通过，返回查找未处理的申请信息
        data:user_Id,            //{"0":11111,"1":11111}传入数据类型     key 为序号 value 为  apply_id
        success:function(data){
            tabledata = data;           //返回数据为上述tabledata结构
        },
        error:
            window.alert("处理失败！"),
        complete:
            drawtable(tabledata)
    })
}
